package IC.Parser;    

import java.util.ArrayList;  
import java.util.List;    
import IC.AST.*;


/**
 * This Class Represent Class data (Fields and Methods)
 * Design as a Singelton class.
 * 
 * @author Liran Levy
 *
 */
public class ClassData  {    
	
	private List<Field> Fields;      
	private List<Method> Methods;            
	
	private static ClassData inst = null;
	
	public static boolean HasInstance()
	{
		return (inst!=null);
	}
	
	public static void CreateNewInstance() throws Exception
	{
    	if (inst==null)
			inst = new ClassData();
    	else
    		throw new Exception("Instance already exist!");
	}

	public static ClassData Inst() throws Exception
	{
		if (inst==null)
			throw new Exception("Instance is not exist!");
		return inst;
	}
	
	//C'tor
	private ClassData() 
	{          
		this.Methods = new ArrayList<Method>();   
		this.Fields = new ArrayList<Field>();             
	}            
	

	 /** 
     * This function get a Type and a list of names and returns a list of fields named Name[i] and Type t. 
     * @param t - Fields Type. 
     * @param FieldNames - List of Field Names.
     * @return List<Field>  
     */ 
	public static List<Field> FieldList(Type t, List<String> FieldNames) {                  
		 List<Field> result = new ArrayList<Field>();                  
		 for (String n : FieldNames)                      
			 result.add(new Field(t, n));                                  
		 return result;          
	 }  
	 
	/** 
     * This function add Method to Methods
     * @param m - Method to add. 
     */ 
	public void addMethod(Method m) 
	{          
		this.Methods.add(m);      
	}
	
	/** 
     * This function add Methods to Methods
     * @param Methods - Method list to add. 
     */ 
	public void addMethod(List<Method> Methods) 
	{          
		this.Methods.addAll(Methods);      
	}  
	
	/** 
     * This function returns Methods and dispose current Methods
     */ 
	public List<Method> disposeMethods() 
	{          
		List<Method> tmp = this.Methods;
		this.Methods = new ArrayList<Method>(); 
		return tmp;
	}
	
	/** 
     * This function add Fields to Fields
     * @param Fields - Field list to add. 
     */ 
	public void addField(List<Field> Fields) 
	{          
		this.Fields.addAll(Fields);      
	}  
	
	/** 
     * This function add Field to Fields
     * @param f - Field to add. 
     */ 
	public void addField(Field f) 
	{          
		this.Fields.add(f);      
	}
	
	/** 
     * This function returns Fields and dispose current Fields
     */ 
	public List<Field> disposeFields() 
	{          
		List<Field> tmp = this.Fields;
		this.Fields = new ArrayList<Field>(); 
		return tmp;
	}
}  